home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / hf^k-6.dms / in.adf / Install.run / GOLDEDDATA / developer / examples / scanner / source / html.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-16  |  5.0 KB  |  156 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   COPYIGHT
  4.  
  5.   ©1995  Dietmar  Eilert  (e-mail:  DIETMAR@TOMATE.TNG.OCHE.DE).  All  Rights
  6.   Reserved.  Code  may not be reused/reproduced without written permission of
  7.   the author.
  8.  
  9.   Dietmar Eilert
  10.   Mies-v-d-Rohe-Str.31, 52074 Aachen, Germany
  11.   E-Mail: DIETMAR@TOMATE.TNG.OCHE.DE
  12.   Tel: +49-(0)241-81665
  13.        +49-(0)2525-7776
  14.   Fax: +49-(0)241-81665
  15.  
  16.   Example: scan handler looking for HTML anchors. Known bug: can not handle
  17.   more than one anchor per line due to the scanner interface design.
  18.  
  19.   Scan handlers are plain functions (LoadSeg'ed by GED): no standard C startup
  20.   code, no library calls. String constants have to be put into the code chunk.
  21.   That's why below a string constant is (ab)used as a buffer instead of using
  22.   a static array.
  23.  
  24.   DICE-C:
  25.  
  26.   dcc html.c -// -l0 -md -ms2 -mRR -o ram:HTML
  27.  
  28.   ------------------------------------------------------------------------------
  29. */
  30.  
  31. #include <exec/types.h>
  32.  
  33. #define UPPER(a) ((a) & 95)
  34.  
  35. ULONG
  36. ScanHandlerHTML(__D0 ULONG len, __A0 char **text, __A1 ULONG *line)
  37. {
  38.     const char *version = "$VER: HTML 1.1 (" __COMMODORE_DATE__ ")";
  39.  
  40.     // minimum strings: <A NAME="X"> or <A HREF="X">
  41.  
  42.     if (len >= 12) {
  43.  
  44.         UBYTE *from, *start, *last;
  45.  
  46.         from = *text;
  47.         last = *text + len;
  48.  
  49.         while ((from + 11) < last) {
  50.  
  51.             // anchor detected ?
  52.  
  53.             if ((*from == '<') && (UPPER(from[1]) == 'A') && (from[2] == ' ')) {
  54.  
  55.                 // 1st try: look for <A HREF="..."> or <A NAME="...">
  56.  
  57.                 for (start = from + 3; start < last; ++start) {
  58.  
  59.                     if (UPPER(*start) == 'H') {
  60.  
  61.                         if (UPPER(start[1]) == 'R') {
  62.  
  63.                             if (UPPER(start[2]) == 'E') {
  64.  
  65.                                 if (UPPER(start[3]) == 'F') {
  66.  
  67.                                     for (start += 4; start < last; ++start) {
  68.  
  69.                                         // look for beginning of link string
  70.  
  71.                                         if (*start == 34) {
  72.  
  73.                                             static UBYTE buffer[255];
  74.  
  75.                                             UBYTE *result = buffer;
  76.  
  77.                                             *result++ = 'H';
  78.                                             *result++ = 'R';
  79.                                             *result++ = 'E';
  80.                                             *result++ = 'F';
  81.                                             *result++ = ' ';
  82.  
  83.                                             for (len = 5, ++start; start < last; ++len) {
  84.  
  85.                                                 if ((*start == 34) || (len == 255)) {
  86.  
  87.                                                     *text = buffer;
  88.  
  89.                                                     return(len);
  90.                                                 }
  91.                                                 else
  92.                                                     *result++ = *start++;
  93.                                             }
  94.  
  95.                                             ++from;
  96.                                         }
  97.                                     }
  98.                                 }
  99.                             }
  100.                         }
  101.                     }
  102.  
  103.                     if (UPPER(*start) == 'N') {
  104.  
  105.                         if (UPPER(start[1]) == 'A') {
  106.  
  107.                             if (UPPER(start[2]) == 'M') {
  108.  
  109.                                 if (UPPER(start[3]) == 'E') {
  110.  
  111.                                     for (start += 4; start < last; ++start) {
  112.  
  113.                                         // look for beginning of link string
  114.  
  115.                                         if (*start == 34) {
  116.  
  117.                                             static UBYTE buffer[255];
  118.  
  119.                                             UBYTE *result = buffer;
  120.  
  121.                                             *result++ = 'N';
  122.                                             *result++ = 'A';
  123.                                             *result++ = 'M';
  124.                                             *result++ = 'E';
  125.                                             *result++ = ' ';
  126.  
  127.                                             for (len = 5, ++start; start < last; ++len) {
  128.  
  129.                                                 if ((*start == 34) || (len == 255)) {
  130.  
  131.                                                     *text = buffer;
  132.  
  133.                                                     return(len);
  134.                                                 }
  135.                                                 else
  136.                                                     *result++ = *start++;
  137.                                             }
  138.  
  139.                                             ++from;
  140.                                         }
  141.                                     }
  142.                                 }
  143.                             }
  144.                         }
  145.                     }
  146.                 }
  147.             }
  148.             else
  149.                 ++from;
  150.         }
  151.     }
  152.  
  153.     return(FALSE);
  154. }
  155.  
  156.